import axios from 'axios'
import { Message } from 'element-ui'
import router from '@/router'
axios.create({
  baseURL: '/api',
  // baseURL: 'http://445z5c0523.qicp.vip/api',
  timeout: 30000
})

// 请求拦截器

axios.interceptors.request.use((config) => {
  const token = localStorage?.getItem('user-token')
  if (token) {
    config.headers.Authorization = token
  }
  return config
})

// 响应拦截器

axios.interceptors.response.use(function (response) {
  if (response.headers?.authorization) {
    localStorage.setItem('user-token', response.headers.authorization)
  }
  if (response.data.code === -1008) {
    router.push('/login')
    Message.error('请重新登陆')
    return Promise.reject(response)
  }
  if (response.data.code === -1) {
    Message.error(response.data.msg)
    return
  }
  return response.data
}, err => {
  Message.error(err)
})

export default axios
